package org.example.sysuser.service.Impl;

import cn.hutool.crypto.digest.BCrypt;
import org.example.Exception.SysUserException;
import org.example.sysuser.entity.SysUser;
import org.example.sysuser.mapper.SysUserMapper;
import org.example.sysuser.service.SysUserService;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

public class SysUserServiceImpl implements SysUserService {

    private final SysUserMapper sysUserMapper;


    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
    /**
     登录
     @param username
     @param password
     @return
     */

    @Override
    public SysUser login(String username, String password) throws SysUserException{
        if (username == null || username.trim().isEmpty()) {
            throw new SysUserException("用户名不能为空");
        }

        if (password == null || password.trim().isEmpty()) {
            throw new SysUserException("密码不能为空");
        }
        try {
            SysUser sysUser = sysUserMapper.login(username, password);
            if (sysUser == null) {
                throw new SysUserException("用户名或密码错误");
            }
            if (!BCrypt.checkpw(password, sysUser.getPassword())) {
                throw new SysUserException("密码错误");
            }

            // 3. 返回用户信息
            return sysUser;
        } catch (RuntimeException e) {
            throw new SysUserException("系统异常，请稍后再试");
        }


    }
/**
    注册用户
     @param sysUser
     @return
 */
    @Override
    public void register(SysUser sysUser) throws SysUserException{
        if(Objects.isNull(sysUser)){
            throw new SysUserException("用户不能为空");
        }
        try{
            String rawPassword = sysUser.getPassword();
            if (rawPassword == null || rawPassword.trim().isEmpty()) {
                throw new SysUserException("密码不能为空");
            }
            // 1. 查询是否已存在相同用户名、手机号、邮箱
            List<SysUser> existingUsers = sysUserMapper.selectSysUser(sysUser);
            // 开关
            boolean userExists = false;
            String conflictMessage = "";
            for (SysUser existing : existingUsers) {
                if (sysUser.getUserName().equals(existing.getUserName())) {
                    userExists = true;
                    conflictMessage = "用户名已存在，禁止注册";
                    break;
                }
                if (sysUser.getPhonenumber() != null && sysUser.getPhonenumber().equals(existing.getPhonenumber())) {
                    userExists = true;
                    conflictMessage = "手机号已存在，禁止注册";
                    break;
                }
                if (sysUser.getEmail() != null && sysUser.getEmail().equals(existing.getEmail())) {
                    userExists = true;
                    conflictMessage = "邮箱已存在，禁止注册";
                    break;
                }
            }
            if (userExists) {
                throw new SysUserException(conflictMessage);
            }
            // 使用 Hutool 的 BCrypt 加密密码
            String hashedPassword = BCrypt.hashpw(rawPassword);
            sysUser.setPassword(hashedPassword);

            sysUserMapper.register(sysUser);
        }catch ( SQLException e){
            throw new SysUserException("注册失败，数据库异常：" + e.getMessage());
        }
    }
    /**
     查询用户
     @param sysUser
     @return
     */

    @Override
    public List<SysUser> selectSysUser(SysUser sysUser) {
        try {
            return sysUserMapper.selectSysUser(sysUser);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
